package com.base.thread.demo_02;

import java.util.Arrays;
import java.util.List;

/**
 * 死锁测试
 * @ClassName: Demo2
 * @Description: 
 * @Author xuyou
 * @DateTime 2019年12月11日 下午7:29:25
 */
public class Demo {

	public static void main(String[] args) {
		List<Integer> list1 = Arrays.asList(1,2,3);
		List<Integer> list2 = Arrays.asList(4,5,6);
		new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (list1) {
					for(Integer integer : list1) {
						System.out.println(integer);
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					synchronized (list2) {
						for(Integer integer : list2) {
							System.out.println(integer);
						}
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}).start();
		
		
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (list2) {
					for(Integer integer : list1) {
						System.out.println(integer);
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					synchronized (list1) {
						for(Integer integer : list2) {
							System.out.println(integer);
						}
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}).start();
		
		
		
	}
	
	
}
